#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/version.h>
#include <AVRBFLY.h>
#include "LCD_functions.h"
#include "LCD_driver.h"
#include "RTC.h"
#include "USART_driver.h"
#include "ADC.h"
#include "dataflash.h"
#include <string.h>
#include "button.h"

#define CLEAR_LCD LCD_Clear();

uint32_t interval_desired = 3;
signed int increment = 0;

//char PowerSaveTimeout = 30;     // Initial value, enable power save mode after 30 min
//unsigned char gButtonTimeout = FALSE;


int main(void)
{
	InitBoard();
	LCD_Init();
	ADC_init();
	//uint16_t adc_output = 0;
	RTC_init();
	Button_Init();
    PORTF |= (1<<PF3);  
    DDRF |=  (1<<DDF3); 
	char Retrieved_data[12];
	unsigned int j;
	
int state_ctrl = 0; 					//Default logging stopped mode: 0 for Logging stopped
int menu_ctrl = 1;						//Menu selection index
int scroll_ctrl = 1;					//scroll selection index for main menu
int sub_menu_scroll_ctrl = 1;			//scroll selection index for sub menu

//Button_push may be used for power saver in which case it should be appropriately set after inner loops
int button_push = 1;					//flag for button check
int hmtoggle = 1;						//Hrs Min Toggle for interval setting, 1 corresponds to min

//Flag for Light intensity mode. 1 corresponds to ON.
int light_on=1;

// the values of h and m should be used for interval calculation
//uint8_t h,m,logstart;							//variables to store hrs and min for the interval rate
h=00;									//default rate 0h5m. these variables will be used in the functions
m=05;
logstart=0;


char min[4], hrs[4], hh[4], mm[4], time[15]; //Character strings to store and display interval rate

strcpy(min, "m");						// Storing units(indices) for the min and hrs strings in the clock 
strcpy(hrs, "h");

char message2_1[25];
strcpy(message2_1, "2.1 Current Interval ");

   while (1) 
   {
   		

		while(menu_ctrl==1)                     //Main Menu index
	{

	if(scroll_ctrl==1)					//Main Menu Option 1
		{
		if(state_ctrl==0)				//state_ctrl=0 when logging stopped
			{
			LCD_puts("1.Start Log", 1);	//Menu Option 1 in logging stopped mode 
			//delay_ds(200);
			}
		else if (state_ctrl==1)			//state_ctrl=1 for logging active
			{
			LCD_puts("1.Stop Log", 1);		//Menu Option 1 in logging running mode
			}
		}
	
	else if(scroll_ctrl==2)				//Main Menu Option 2
		{
		
		LCD_puts("2.Define Interval", 1);
		//delay_ds(200);
		}

	
	else if(scroll_ctrl==3)				//Main Menu Option 3
		{
		
		LCD_puts("3.Read Log", 1);

		} 

	else if(scroll_ctrl==4)				//Main Menu Option 4
		{
		
		LCD_puts("4.Send to PC", 1);

		} 

	else if(scroll_ctrl==5)				//Main Menu Option 5
		{
		if(light_on==1)				//Checking for Light Intensity Triggering on/off: 1 corresponds to ON
			{
			LCD_puts("5.Turn Off Light Trigger", 1);
			}
		else if(light_on==0)
			{
			LCD_puts("5.Turn ON Light Trigger", 1);
			}

		} 


	while(button_push==1)						//checking for scrolling actions
		{

		if(!(PINB & (1 << 7)))
	{	while(!(PINB & (1 << 7)));				//Joystick down
			{
			CLEAR_LCD
			//delay_ds(50);
			scroll_ctrl++;
			button_push=0;						//button check loop exit criteria
			}
		}
		if(!(PINB & (1 << 6)))
	{	while(!(PINB & (1 << 6)));				//Joystick up
			{	
			CLEAR_LCD
			//delay_ds(50);
			scroll_ctrl--;
			button_push=0;						//button check loop exit criteria
			}
		}
		
		if(!(PINB & (1 << 4)))				//Center Button
			{
			
			while(!(PINB & (1 << 4)));
			{//delay_ds(50);
			CLEAR_LCD
			button_push=0;						//button check loop exit criteria
			
			if(scroll_ctrl==1)					//selecting first menu option i.e logging
				{
				menu_ctrl=2;
				state_ctrl = (!state_ctrl);
				if(state_ctrl)
				{
				logstart = 1;
				RTC_init();
				}
				else
				{
				logstart = 0;
				cbiBF(TIMSK2, TOIE2);     // enable Timer2 overflow interrupt
				button_push=0;
				menu_ctrl=1;
				state_ctrl = 0; 					//Default logging stopped mode: 0 for Logging stopped
				CLEAR_LCD								//Menu selection index
				scroll_ctrl = 1;					//scroll selection index for main menu
				sub_menu_scroll_ctrl = 1;


				}
			//	state_ctrl = (!state_ctrl);
				}
			else if(scroll_ctrl==2)					//selecting 2nd option i.e changing interval
				{
				menu_ctrl=3;						
				}
			else if(scroll_ctrl==3)					//selecting 3rd option i.e reading log
				{
				menu_ctrl=4;						
				}
			else if(scroll_ctrl==4)					//selecting 4th option i.e UART
				{
				menu_ctrl=5;
				}
			else if(scroll_ctrl==5)					//selecting 5th option
				{
				light_on=!light_on;
				}
			
			}}
		
		}
		
	button_push=1;								//resetting button check loop exit criteria

	if(scroll_ctrl<1)							//controlling scroll limits
		{
		scroll_ctrl=1;
		}

	else if(scroll_ctrl>5)
		{
		scroll_ctrl=5;
		}


		//LCD_NUMBER(scroll_ctrl)				//LINE FOR DEBUGGING

	}

//LCD_puts(menu_ctrl);
//delay_ds(300);

//**************************************************************************************************************
//start of 1st sub menu: Start/Stop Log
//**************************************************************************************************************
while(menu_ctrl==2)								//Sub Menu Index
	{

	if(sub_menu_scroll_ctrl==1)					//Sub Menu Option 1
		{
		if(state_ctrl==1)						//Display interval if logging is active
			{
			strcpy(time, "");								//resetting or emptying the string
			strcpy(message2_1, "2.1 CurrentInterval ");	//resetting the string
			itoa(h, hh, 10);								//converting the value in h into a char string hh
			strcat(hh, hrs);								//appending 'H' at the end of digits in hh

			itoa(m, mm, 10);								//converting the value in m into a char string mm
			strcat(mm, min);								//appending 'M' at the end of digits in mm

			strcat(time, hh);								//storing hh value with a trailing 'H' in the timestamp
			strcat(time, mm);								//appending mm value with a trailing 'M' in the timestamp
			//LCD_Colon(2);

			//LCD_puts("1.1 Current Interval:"); 			//add the current interval variable here
			strcat(message2_1,time);
			LCD_puts(message2_1, 1);							//Displaying Time stamp message
			
			}

		else if(state_ctrl==0)					//Display Log stop Message
			{
			LCD_puts("1.1 Logging Stopped", 1);
			}
		}

	else if(sub_menu_scroll_ctrl==2)			//Sub Menu Option 2
		{
		LCD_puts("1.2 Define Interval", 1);			// add code to navigate back to 3rd menu
		}
/* replaced with left scroll functionality
	else if(sub_menu_scroll_ctrl==3) 			//Sub Menu Option 3
		{
		LCD_puts("1.3 Back");								
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{
		if(!(PINB & (1 << 7)))
	{	while(!(PINB & (1 << 7)));						//Joystick down
			{
			CLEAR_LCD
			//delay_ds(50);
			sub_menu_scroll_ctrl++;
			button_push=0;
			}
		}

		if(!(PINB & (1 << 6)))
	{	while(!(PINB & (1 << 6)));						//Joystick down
			{
			CLEAR_LCD
			delay_ds(50);
			sub_menu_scroll_ctrl--;
			button_push=0;								//button check loop exit criteria
			}
		}


		if(!(PINB & (1 << 4)))
	{	while(!(PINB & (1 << 4)));						//Center Button
			{
			//delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
/* replaced with left scroll functionality
			if(sub_menu_scroll_ctrl==3)					//selecting 3rd option
				{
				menu_ctrl=1;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
*/
			if(sub_menu_scroll_ctrl==2)					//selecting 2rd option
				{
				menu_ctrl=3;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}
		}


		if(!(PINE & (1 << 2)))
		{while(!(PINE & (1 << 2)));						//Left Button 
			{
			//delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			sub_menu_scroll_ctrl = 1;				
			button_push=0;								//button check loop exit criteria
			}
		}		

		}
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>2)
		{
		sub_menu_scroll_ctrl=2;
		}

	
	}

//**************************************************************************************************************
//start of 3rd sub menu: Change Interval
//**************************************************************************************************************
while(menu_ctrl==3)										//Sub Menu Index
	{

	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		strcpy(time, "");								//resetting or emptying the string
		itoa(h, hh, 10);								//converting the value in h into a char string hh
		strcat(hh, hrs);								//appending 'H' at the end of digits in hh

		itoa(m, mm, 10);								//converting the value in m into a char string mm
		strcat(mm, min);								//appending 'M' at the end of digits in mm

		strcat(time, hh);								//storing hh value with a trailing 'H' in the timestamp
		strcat(time, mm);								//appending mm value with a trailing 'M' in the timestamp
		//LCD_Colon(2);

		LCD_puts(time, 1);									//Displaying Time stamp
		LCD_UpdateRequired(TRUE, 0);					//Disabling LCD Scroll Mode

		//LCD_puts("2.1 HHMM");				//add the current interval variable here
		}
/*  replaced with left scroll functionality
	else if(sub_menu_scroll_ctrl==2) 					//Sub Menu Option 2
		{
		LCD_puts("2.2 Back");								
		
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{


		if(!(PINB & (1 << 7)))
	{	while(!(PINB & (1 << 7)));						//Joystick down
			{
			//delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m-1;
					if(m<1)							//resetting min to 0 after 59
						{
						m=59;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h-1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=24;
						}
					}
				//sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}
		}

		if(!(PINB & (1 << 6)))
	{	while(!(PINB & (1 << 6)));						//Joystick up
			{//delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m+1;
					if(m>59)							//resetting min to 0 after 59
						{
						m=0;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h+1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=0;
						}
					}
				//sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			}
			}
/*		
		while(!(PINB & (1 << 4)))						//Center Button
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			
			if(sub_menu_scroll_ctrl==1)					//Setting interval in first option
				{
				if(hmtoggle==1)							//setting minutes
					{
					m=m+1;
					if(m>59)							//resetting min to 0 after 59
						{
						m=0;
						}
					}
			
				else if(hmtoggle==0)					//setting hrs
					{
					h=h+1;
					if(h>24)							//resetting hrs to 0 after 24
						{
						h=0;
						}
					}
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				}
			
 replaced with left scroll functionality
			else if(sub_menu_scroll_ctrl==2)					//selecting 2nd option
				{
				menu_ctrl=1;
				sub_menu_scroll_ctrl = 1;			//setting submenu scroll to first position
				LCD_UpdateRequired(TRUE, 1);					//Enabling LCD Scroll Mode
				}
				


			}
*/

		if(!(PINE & (1 << 2)))
	{	while(!(PINE & (1 << 2)));						//Left Button 
			{
			//delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			sub_menu_scroll_ctrl = 1;				
			button_push=0;								//button check loop exit criteria
			scroll_ctrl=2;
			}
		}

		if(!(PINE & (1 << 3)))
		{	while(!(PINE & (1 << 3)));						//Right Button
			{
			//delay_ds(50);
			//button_push=0;								//button check loop exit criteria
			if(sub_menu_scroll_ctrl==1)
				{
				hmtoggle=!hmtoggle;
				}
			}
		}
	}
	
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

	
	}							//End of Submenu

//***********************************************************************************************************
// 4th Submenu: Read Log
//************************************************************************************************************

while(menu_ctrl==4)										//Sub Menu Index
	{
			
				dataretrieve(Retrieved_data);
				LCD_puts(Retrieved_data, 1);
				//_delay_ms(2000);
		
	
	
	
	
	//TO BE REPLACED BY ACTUAL RECORD DATA AND A CONDITION TO DISPLAY THE BELOW PROMPT WHEN NEEDED
	//LCD_puts("No Records to Display", 1); 					


/*	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		}
*/
	while(button_push==1)								//checking for scrolling actions
		{
		
		if(!(PINB & (1 << 7)))
	{	while(!(PINB & (1 << 7)));						//Joystick down
			{
			//delay_ds(50);
			CLEAR_LCD
			button_push=0;
											//button check loop exit criteria
			//Fetch Next record here
				increment = 12;

			}
		}
		
		if(!(PINB & (1 << 6)))
		{while(!(PINB & (1 << 6)));						//Joystick up
			{
			//delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Previous Record Here
			increment = -12;
			}
			}

			if(!(PINE & (1 << 2)))
		{while(!(PINE & (1 << 2)));						//Left Button 
			{
			//delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			button_push=0;								//button check loop exit criteria
			scroll_ctrl=3;
			}
			}
		}

	
		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

	
	}												//End of Submenu

//**************************************************************************************************************
// 5th Submenu: Send to PC
//************************************************************************************************************

while(menu_ctrl==5)										//Sub Menu Index
	{

	LCD_puts("Sending to PC", 1); 	
	
	USARTinit();
	dataretrieve_pc(Retrieved_data);
	
	
	
					

//Initiate UART Communication here using menu_ctrl=5

/*	if(sub_menu_scroll_ctrl==1)							//Sub Menu Option 1
		{
		}
*/
/*	while(button_push==1)								//checking for scrolling actions
		{

		while(!(PINB & (1 << 7)))						//Joystick down
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Next record here
			}

		while(!(PINB & (1 << 6)))						//Joystick up
			{
			delay_ds(50);
			CLEAR_LCD
			button_push=0;								//button check loop exit criteria
			//Fetch Previous Record Here
			}
*/
		while(!(PINE & (1 << 2)))						//Left Button 
			{
			delay_ds(50);
			menu_ctrl=1;								//Return to main Menu
			//button_push=0;								//button check loop exit criteria
			scroll_ctrl=4;
			//Terminate USART Protocol here
			}

//		}

	
/*		
	button_push=1;										//resetting button check loop exit criteria

	if(sub_menu_scroll_ctrl<1)							//controlling scroll limits
		{
		sub_menu_scroll_ctrl=1;
		}

	else if(sub_menu_scroll_ctrl>1)
		{
		sub_menu_scroll_ctrl=1;
		}

*/	

	}

		}

   return 0; 

}

